package org.web.abc.stockanalysis.server.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.web.abc.stockanalysis.server.biz.impl.MainTaskBizImpl;
import org.web.abc.stockanalysis.server.biz.impl.BatchTaskDO;
import org.web.abc.stockanalysis.server.biz.impl.TaskDefinitionEnum;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static java.time.format.DateTimeFormatter.ofPattern;


@Controller
@RequestMapping("task")
public class TaskController {

    private static final Logger logger = LogManager.getLogger(TaskController.class);

    @Resource
    private MainTaskBizImpl mainTaskBiz;

    private static boolean isRunning = false;

    @RequestMapping("doTask")
    @ResponseBody
    public String doTask(String stockCode) {
        if (isRunning) {
            return "task is running";
        }
        isRunning = true;
        // 定义目标格式
        DateTimeFormatter formatter = ofPattern("yyyyMMdd");
        // 格式化日期
        LocalDate today = LocalDate.now();
        String executeDate = today.format(formatter);
        BatchTaskDO batchTaskDO = new BatchTaskDO(TaskDefinitionEnum.STOCK_DAY_PRICE_FULL);
        batchTaskDO.setStockCode(stockCode);
        batchTaskDO.setExecuteDate("20250818");
        batchTaskDO.getExtendMap().put("start_date", "20250815");
        batchTaskDO.getExtendMap().put("end_date", "20250818");

        mainTaskBiz.doTask(batchTaskDO);
        isRunning = false;
        return "success";
    }


}

